﻿/////////////////////////////////////////////////////////////////
//
// Silverlight + FarSeer Physics Helper
//
// by Andy Beaulieu - http://www.andybeaulieu.com
//
// LICENSE: This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ANY 
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS
// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
/////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Text;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Controllers;
using FarseerPhysics.Collision;
using Microsoft.Xna.Framework;


namespace Spritehand.FarseerHelper
{

    public class FluidContainerMain
    {
        public static readonly DependencyProperty FluidControllerProperty = DependencyProperty.RegisterAttached("FluidController", typeof(FluidContainerMain), typeof(FrameworkElement), new PropertyMetadata(null));

        PhysicsControllerMain _physicsControllerMain;

        private System.Windows.Point _bottomRightPoint;
        private System.Windows.Point _bottomLeftPoint;

        //public WaveController WaveControllerObject { get; set; }

        public static FluidContainerMain GetFluidController(DependencyObject target)
        {
            return (FluidContainerMain)target.GetValue(FluidContainerMain.FluidControllerProperty);
        }

        public static void SetFluidContainer(DependencyObject target, FluidContainerMain value)
        {
            target.SetValue(FluidContainerMain.FluidControllerProperty, value);
        }

        public FluidContainerMain()
        {

        }

        private Shape _visualElement;
        public Shape VisualElement
        {
            get { return _visualElement; }
            set
            {
                _visualElement = value;
            }
        }


        private double _fluidDensity = 0.002;
        public double FluidDensity
        {
            get { return _fluidDensity; }
            set { _fluidDensity = value; }
        }

        private double _linearDragCoefficient = 0.002;
        public double LinearDragCoefficient
        {
            get { return _linearDragCoefficient; }
            set { _linearDragCoefficient = value; }
        }

        private double _rotationalDragCoefficient = 0.001;
        public double RotationalDragCoefficient
        {
            get { return _rotationalDragCoefficient; }
            set { _rotationalDragCoefficient = value; }
        }

        private int _gravityHorizontal = 0;
        public int GravityHorizontal
        {
            get { return _gravityHorizontal; }
            set { _gravityHorizontal = value; }
        }

        private int _gravityVertical = 500;
        public int GravityVertical
        {
            get { return _gravityVertical; }
            set { _gravityVertical = value; }
        }




        public void Initialize(PhysicsControllerMain physicsController)
        {
            _physicsControllerMain = physicsController;
            AddFluidContainer();
        }

        public void AddFluidContainer()
        {
            double x = Convert.ToDouble(this.VisualElement.GetValue(Canvas.LeftProperty));
            double y = Convert.ToDouble(this.VisualElement.GetValue(Canvas.TopProperty));
            float width = (float)this.VisualElement.ActualWidth;
            float height = (float)this.VisualElement.ActualHeight;



            Vector2 vecTopLeft = _physicsControllerMain.ScreenToWorld(new System.Windows.Point(x, y));
            Vector2 vecBottomRight = _physicsControllerMain.ScreenToWorld(new System.Windows.Point(x + width, y + height));


            AABB container = new AABB(vecTopLeft, vecBottomRight);
            BuoyancyController buoyancy = new BuoyancyController(container, (float)this.FluidDensity, (float)this.LinearDragCoefficient, (float)this.RotationalDragCoefficient, new Vector2((float)this.GravityHorizontal, (float)this.GravityVertical));


            _physicsControllerMain.Simulator.AddController(buoyancy);
        }


    }
}
